package com.npjscg.medicalpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.common.Result;

import com.npjscg.medicalpro.entity.Check;
import com.npjscg.medicalpro.entity.Notification;
import com.npjscg.medicalpro.entity.User;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.mapper.NotificationMapper;
import com.npjscg.medicalpro.mapper.UserMapper;
import com.npjscg.medicalpro.service.INotificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-06-29
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements INotificationService {

    @Resource
    private NotificationMapper notificationMapper;

    @Resource
    private UserMapper userMapper;



    @Override
    public IPage getInfo(Page<Notification> page, int uid, int check) {
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("is_check", check);

        return notificationMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void change(Notification notification) {
        User user = userMapper.selectById(notification.getUid());
        Notification notification1 = notificationMapper.selectById(notification.getId());
        if (user == null)
            throw new ServiceException(Constants.CODE_600, "没有此用户信息");
        else if (notification1 == null) {
            throw new ServiceException(Constants.CODE_600, "查无通知");
        }
        else{
            UpdateWrapper<Notification> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", notification.getId());
            updateWrapper.set("is_check", notification.getIsCheck());
            update(updateWrapper);
        }

    }

    @Override
    public void addNotice(Notification notification) {
        User user = userMapper.selectById(notification.getUid());
        if (user == null)
            throw new ServiceException(Constants.CODE_600, "数据库中没有此用户");
        else
            save(notification);
    }

    @Override
    public void empty(int uid) {
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("is_check", 1);
        notificationMapper.delete(queryWrapper);
    }
}
